package com.css.fxfzypg.modules.resultsManagement.service.impl;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.hypergraph.service.HypergraphService;
import com.css.fxfzypg.modules.preassesstaskmanage.vo.PreVo;
import com.css.fxfzypg.modules.resultsManagement.entity.MapTemplate;
import com.css.fxfzypg.modules.resultsManagement.repository.MapTemplateRepository;
import com.css.fxfzypg.modules.resultsManagement.service.MapManagementService;
import com.css.fxfzypg.modules.resultsManagement.service.MapTemplateService;
import com.css.fxfzypg.modules.surveytaskmanage.service.FieldSurveyTaskService;
import com.css.fxfzypg.util.*;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class MapTemplateServiceImpl implements MapTemplateService {
    @Resource
    MapTemplateRepository mapTemplateRepository;
    @Resource
    SUserService sUserService;
    @Resource
    MapManagementService mapManagementService;
    @Resource
    FieldSurveyTaskService fieldSurveyTaskService;
    @Resource
    private HypergraphService hypergraphService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse<Object> saveMapTemplate(MapTemplate mapTemplate) {
        MapTemplate mapTemplate1=mapTemplateRepository.getByTypeIdAndFormat(mapTemplate.getMapTypeId(),mapTemplate.getFormat());
        if (PlatformObjectUtils.isNotEmpty(mapTemplate1)){
            return RestResponse.fail("保存失败,该图件类型下已有相同版式");
        }
        //上传模板文件到超图模板目录
        String smTemplateName = null;
        RestResponse res = hypergraphService.uploadTemplateFile(mapTemplate.getTemplateId(),mapTemplate.getFormat());
        if(res.getCode()==200){
            smTemplateName = (String) res.getData();
        }else{
            return res;
        }
        if (PlatformObjectUtils.isEmpty(smTemplateName)){
            return RestResponse.fail("上传模板文件到超图模板目录失败");
        }
        mapTemplate.setSmTemplateName(smTemplateName);//超图Web打印模板名称
        mapTemplate.setId(UUIDGenerator.getUUID());
        mapTemplate.setDelFlag(YNEnum.N.toString());
        mapTemplate.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        mapTemplate.setCreateUser(PlatformSessionUtils.getUserId());
        if (PlatformObjectUtils.isNotEmpty(PlatformSessionUtils.getUserId())) {
            String userJson = sUserService.getSUser(PlatformSessionUtils.getUserId());
            SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
            if (sUser != null) {
                mapTemplate.setUploader(sUser.getUserName());
            }
        }
        mapTemplate.setUploadTime(PlatformDateUtils.getCurrentTimestamp());
        mapTemplateRepository.saveMapTemplate(mapTemplate);
        return RestResponse.succeed();
    }

    @Override
    public MapTemplate getById(String id) {
        return mapTemplateRepository.getById(id);
    }

    @Override
    public void deleteById(String id) {
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        mapTemplateRepository.deleteById(id,currentTimestamp);
    }

    @Override
    public MapTemplate update(MapTemplate mapTemplate) {
        if (PlatformObjectUtils.isNotEmpty(PlatformSessionUtils.getUserId())) {
            String userJson = sUserService.getSUser(PlatformSessionUtils.getUserId());
            SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
            if (sUser != null) {
                mapTemplate.setUploader(sUser.getUserName());
            }
        }
        mapTemplate.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        mapTemplate.setUpdateUser(PlatformSessionUtils.getUserId());
        return mapTemplateRepository.update(mapTemplate);
    }

    @Override
    public Map<String, Object> queryMapTemplates(MapTemplate mapTemplate, int curPage, int pageSize) {
        Map<String, Object> map = mapTemplateRepository.queryMapTemplates(mapTemplate, curPage, pageSize);
        return map;
    }

    @Override
    public List<Map<String, Object>> query(String reportType) {
        List<MapTemplate> mapTemplates=mapTemplateRepository.query(reportType);
        List<Map<String, Object>> maps = new ArrayList<>();
        Map<String, List<MapTemplate>> collect = mapTemplates.stream().collect(Collectors.groupingBy(MapTemplate::getMapType));
        Set<String> strings = collect.keySet();
        for (String string : strings) {
            List<MapTemplate> mapTemplates1 = collect.get(string);
            Map<String, Object> hashMap = new HashMap();
            hashMap.put("type",string);
            hashMap.put("template",mapTemplates1);
            hashMap.put("typeId",mapTemplates1.get(0).getMapTypeId());
            maps.add(hashMap);
        }
        return maps;
    }

    @Override
    public List getTask(String reportType) {
        if ("0".equals(reportType)){
            List<PreVo> list = mapManagementService.getFuTask();
            return list;
        }
        List<PreVo> fuTask = fieldSurveyTaskService.getFuTask();
        return fuTask;
    }
}
